package com.yt.aop;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.yt.annotation.Easy;
@Component
@Aspect
public class MyAspectWeave{
	
	
	@Pointcut("execution(public * com.yt.aop.*.*(..))")
	public void log() {
    }
	
	
	@Before("log()&&@annotation(es)")
	public void before(JoinPoint jp,Easy es) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		System.out.println(this.getClass().getSimpleName()+"前=====>>>>");
		//System.out.println("目标对象"+jp.getSignature().getDeclaringType().getSimpleName());
		//jp.getSignature().getName();//得到方法名
		Object target = jp.getTarget(); //得到目标对象
       // jp.getArgs(); //得到方法参数
//		jp.getSignature().getName().
       /*for(Method m :target.getClass().getDeclaredMethods()){
    	   boolean annotationPresent = m.isAnnotationPresent(Easy.class);
    	   System.out.println(m.getName()+":"+annotationPresent);
       }*/
		Method m = target.getClass().getDeclaredMethod(jp.getSignature().getName());
		
		//m.getParameterTypes();
		
		//System.out.println("参数类型:"+jp.getSignature().getDeclaringTypeName());
		
	
		System.out.println(m.getName()+"==>"+m.isAnnotationPresent(Easy.class));
		//System.out.println(this.getClass().getSimpleName()+"==>"+es.value());
        
		System.out.println(m.invoke(target, "aop in change"));
		
	}
	
	@AfterReturning(pointcut="log()",returning="object")
	public void after(Object object){
		
		System.out.println(this.getClass().getSimpleName()+"==>"+object.toString());
	}
	
	@Before("@annotation(es)")
	public void interAnnotation(Easy es){
		System.out.println(this.getClass().getSimpleName()+"拦截了注解方法===>"+es.value());
	}
}
